package com.edu.controller.front.diningcar;

import com.edu.entity.Diningcar;
import com.edu.entity.Food;
import com.edu.entity.User;
import com.edu.service.diningcar.DiningCarService;
import com.edu.service.diningcar.DiningCarServiceImpl;
import com.edu.service.food.FoodService;
import com.edu.service.food.FoodServiceImpl;
import com.edu.service.user.UserService;
import com.edu.service.user.UserServiceImpl;
import com.edu.utils.GetLoginUser;
import com.edu.utils.ParamFormat;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ZCN
 * @version 1.0
 * @date 2021/11/23 9:32
 */
@WebServlet("/front/adddiningcar")
public class AddCarServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    private FoodService foodService = new FoodServiceImpl();
    private DiningCarService diningCarService = new DiningCarServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            ParamFormat.format(req, resp);
            // 拿到foodid
            Integer foodid = Integer.valueOf(req.getParameter("id"));
            // 获取登录用户
            User user = GetLoginUser.getUser(req);
            // 准备数据
            Diningcar diningcar = new Diningcar();
            diningcar.setUserid(user.getId());
            diningcar.setFoodid(foodid);

            Food food = foodService.findOne(foodid);
            BeanUtils.copyProperties(diningcar,food);

            diningCarService.insert(diningcar);

            resp.sendRedirect("/front/querydiningcar");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
